﻿using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntify;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace biaoqian.CSwnjian
{
    internal class Cangku
    {
        Class1 one=new Class1();
        public DataTable Jins(string Kehu, string Hagn, string wuliao, string Danhao)
        {
            K3CloudApi client = new K3CloudApi();
            //业务对象标识
            string formId = "SAL_DELIVERYNOTICE";
            //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
            string fieldKeys = "FBillNo,FCustomerID.FName,FMaterialID.FNumber,FMaterialName,FMateriaModel,FQty,FPriceUnitId.FName,FDate,FCustMatID.FNumber";
            //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式  and FCancelStatus='A'  and FPlanDeliveryDate >'" + currentDateTime + "'
            var param = new QueryParam()
            {
                FormId = formId,
                FieldKeys = fieldKeys,
                FilterString = "FCustomerID.FName like'%" + Kehu + "%'and FMaterialID.FNumber like'%" + wuliao + "%' and FBillNo like '%"+ Danhao + "%'",
                TopRowCount = 0,
                StartRow = 0,
                Limit = 120,
                OrderString = "FBillNo",
            };
            //调用接口
            var result = client.ExecuteBillQuery(param.ToJson());
            var dataTable2 = one.DAtaijs();
            var ssjinf = 0;
            for (int i = 1; i < result.Count; i++)
            {
                var shuj = result[i][7] == null ? "" : result[i][7].ToString(); //时间
                if (shuj == "")
                {
                    result[i][7] = result[i - 1][7];
                }
            }
            foreach (var item in result)
            {
                ssjinf++;
                var owulia = item[2].ToString();
                var Bianm = item[0].ToString();
                //var TImes = item[7] == null ? "" : item[7].ToString();
                DataRow[] dataRow = dataTable2.Select("FMaterial='" + owulia + "'and encoding='"+ Bianm + "'");
                if (dataRow.Count() > 0)
                {
                    var shnd = int.Parse(item[5].ToString());
                    foreach (var item2 in dataRow)
                    {
                        var shu = int.Parse(item2[5].ToString());
                        shu += shnd;
                        item2[5] = shu;
                    }
                }
                else
                {
                    dataTable2.Rows.Add(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7], item[8]);
                }
            }
            DataTable dataTable23 = one.DAtaijs();
            if (Hagn != "")
            {
                var ji = int.Parse(Hagn);
                DataRow kol = dataTable2.Rows[ji - 1];//符合条件的行
                dataTable23.Rows.Add(kol.ItemArray);
            }
            else
            {
                dataTable23 = dataTable2.Copy();
            }
            return dataTable23;
        }
    }
}
